import axios, { AxiosResponse } from "axios";
import { MessageModel } from "../models/Message";
import { UserModel } from "../models/User";

class MessageApis {
    queryMessageList(id: string): Promise<Array<MessageModel>> {
        let promise: Promise<Array<MessageModel>> = new Promise((resolve, reject) => {
            let user: UserModel = {
                id: id
            };
            axios.get('/api/message', { params: user })
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code == 200) {
                        let messageList: Array<MessageModel> = reslut.data.data;
                        resolve(messageList);
                    } else {
                        resolve([]);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    updateMessage(id: number, userId: string): Promise<boolean> {
        let promise: Promise<boolean> = new Promise((resolve, reject) => {
            let message: MessageModel = {
                id: id,
                user: userId
            };
            axios.post('/api/message', message)
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code == 200) {
                        resolve(true);
                    } else {
                        resolve(false);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }
}

export const messageApis: MessageApis = new MessageApis();